package com.yyj.usercenter.common.util;

import org.springframework.stereotype.Component;

@Component("page")
public class PageUtil
{
	//总记录数
	private int totalRecord;
	//每页多少记录
	private int pageSize=10; 
	//上一页
	private int prePage;
	//下一页
	private int nextPage;
	//当前页
	private int currPage;
	//总页数
	private int totalPage;
	//起始位置（sql语句中用）
	private int currStartRecord;
	
	
	public PageUtil()
	{
		super();
	}
	
	
	public PageUtil(int totalRecord, int pageSize,int currPage)
	{
		super();
		this.totalRecord = totalRecord;
		this.pageSize = pageSize;
		this.currPage = currPage;
	}


	public int getTotalRecord()
	{
		return totalRecord;
	}
	public void setTotalRecord(int totalRecord)
	{
		this.totalRecord = totalRecord;
	}
	
	
	public int getPageSize()
	{
		return pageSize;
	}
	public void setPageSize(int pageSize)
	{
		this.pageSize = pageSize;
	}
	
	
	public int getCurrPage()
	{
		//如果当前页传过来是负数或0
		if(this.currPage<1){
			return 1;
		}
		//如果当前页传过来大于总页数（总页数不能是0，影响起始位置）
		if(this.currPage>this.totalPage&&this.totalPage>0){
			return getTotalPage();
		}
		return currPage;
	}
	public void setCurrPage(int currPage)
	{
		this.currPage = currPage;
	}
	
	//获得前一页
	public int getPrePage()
	{
		this.prePage=this.currPage-1;
		if(this.prePage<1){
			return 1;
		}
		return prePage;
	}
	//获得下一页
	public int getNextPage()
	{
		this.nextPage=this.currPage+1;
		//this.totalPage>0避免算起始位置时，出现负数
		if(this.nextPage>this.totalPage && this.totalPage>0){
			return getTotalPage() ;
		}
		return nextPage;
	}
	//获得总页数
	public int getTotalPage()
	{
		this.totalPage=(int)Math.ceil(1.0*this.totalRecord/this.pageSize);
		return totalPage;
	}
	//获得sql搜索起始值
	public int getCurrStartRecord()
	{
		this.currStartRecord=(getCurrPage()-1)*this.pageSize;
		return currStartRecord;
	}
	@Override
	public String toString()
	{
		return "PageUtil totalRecord=" + getTotalRecord() + ", pageSize=" + getPageSize() + ", prePage=" + getPrePage()
				+ ", nextPage=" + getNextPage() + ", currPage=" + getCurrPage() + ", totalPage=" + getTotalPage()
				+ ", currStartRecord=" + getCurrStartRecord();
	}
	
	public static void main(String[] args)
	{
		PageUtil page=new PageUtil(20,5, 0);
		System.out.println(page);
	}
}
